package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Teacher;
import com.itheima.edu.info.manager.service.TeacherService;

import java.util.Scanner;

public abstract class BaseTeacherController {
    Scanner sc = new Scanner(System.in);

    private TeacherService teacherService = new TeacherService();

    public void start() {

        while (true){
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            String choice = sc.next();

            switch (choice){
                case "1":
                    //System.out.println("添加老师");
                    addTeacher();
                    break;
                case "2":
                    //System.out.println("删除老师");
                    deleteTeacherById();
                    break;
                case "3":
                    //System.out.println("修改老师");
                    updataTeacher();
                    break;
                case "4":
                    //System.out.println("查看老师");
                    findAllTeacher();
                    break;
                case "5":
                    //System.out.println("退出");
                    System.out.println("感谢您使用老师管理系统，再见！");
                    return;
                default:
                    System.out.println("您的输入有误，请重新输入！");
                    break;
            }
        }
    }

    //修改老师
    private void updataTeacher() {
        //判断输入的老师ID是否存在
        String updataId = inputTeacherId();
        //调用底层方法对指定ID老师信息进行修改
        Teacher teacher = inputTeacherInfo(updataId);
        teacherService.updataTeacher(teacher, updataId);
        System.out.println("修改成功！");
    }

    //删除老师
    private void deleteTeacherById() {
        //调用底层方法，判断输入老师ID是否存在于老师数组中
        String deleteId = inputTeacherId();
        //调用底层方法，删除指定ID对象
        teacherService.deleteTeacherById(deleteId);
        System.out.println("删除成功！");
    }

    //查看老师
    private void findAllTeacher() {
        //调用底层方法，拿到老师对象数组
        Teacher[] teachers = teacherService.findAllTeacher();
        //判断数组中元素是否为空，并给出相应提示
        if (teachers == null){
            System.out.println("学生信息不存在，请重新输入！");
            return;
        }
        //遍历数组，并打印不为空的数组元素
        System.out.println("老师ID\t\t姓名\t年龄\t生日");
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher = teachers[i];
            //输出除了空元素外的所有元素
            if (teacher != null){
                System.out.println(teacher.getId() + "\t" + teacher.getName() + "\t" + teacher.getAge() + "岁" + "\t" + teacher.getBirthday());
            }
        }
    }

    //添加老师
    private void addTeacher() {
        //调用底层方法，判断添加的id是否存在
        String id;
        while (true){
            System.out.println("请输入学生ID：");
            id = sc.next();
            boolean exists = teacherService.isExists(id);
            if (exists){
                System.out.println("您输入的ID已被占用，请重新输入！");
            }else {
                break;
            }
        }
        //将学生对象进行封装
        Teacher teacher = inputTeacherInfo(id);
        // 调用底层方法进行添加
        boolean flag = teacherService.addTeacher(teacher,id);
        if (flag){
            System.out.println("添加成功！");
        }else {
            System.out.println("添加失败！");
        }
    }

    //判断输入ID是否存在
    public String inputTeacherId(){
        //调用底层方法，判断输入老师ID是否存在于老师数组中
        String id;
        while (true){
            System.out.println("请输入老师ID：");
            id = sc.next();
            boolean exists = teacherService.isExists(id);
            if (!exists){
                System.out.println("您输入的ID不存在，请重新输入！");
            }else {
                break;
            }
        }
        return id;
    }

    //将学生对象进行封装
    public abstract Teacher inputTeacherInfo(String id);
}
